package com.googlecode.activitiportal.controller;

import java.io.Serializable;
import java.util.List;

import org.activiti.engine.FormService;
import org.activiti.engine.form.StartFormData;
import org.activiti.engine.form.TaskFormData;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.googlecode.activitiportal.service.ProcessService;
import com.googlecode.activitiportal.util.SecurityUtil;

@Controller
public class ProcessController implements Serializable {

	private static final long serialVersionUID = 1L;
	private ProcessDefinition currentProcessDefinition;
	private Task currentTask;
//	private Map<String, String> processVariables;

	@Autowired
	private ProcessService processService;

	@Autowired
	private FormService formService;
	
	@RequestMapping(value = "/inbox", method = RequestMethod.GET)
	public String inbox(ModelMap modelMap) {
		List<Task> taskList = getPersonalTaskList();
		modelMap.addAttribute("taskList", taskList);
		return "secure/inbox";
	}
	
	@RequestMapping(value = "/processes", method = RequestMethod.GET)
	public String processes(ModelMap modelMap) {
		List<ProcessDefinition> processes = getProcessDefinitionList();
		modelMap.addAttribute("processes", processes);
		return "secure/processes";
	}

	public List<ProcessDefinition> getProcessDefinitionList() {
		return getProcessService().getProcessDefinitionList();
	}

	public String showStartForm(String processDefinitionId) {
		StartFormData startFormData = formService.getStartFormData(processDefinitionId);
		String formKey = startFormData.getFormKey();
		ProcessDefinition processDefinition = processService.findProcessDefinitionById(processDefinitionId);
		this.setCurrentProcessDefinition(processDefinition);
		return formKey;
	}

	public String startProcessInstance(String processDefinitionId) {
		ProcessInstance processInstance = getProcessService().startProcessInstanceById(processDefinitionId);
		String id = processInstance.getId();
		
//    formService.submitStartFormData(processDefinitionId, processVariables);
		
		String mensagem = "Processo " + id + " iniciado com sucesso!";
//		FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, "Aviso", mensagem);
//		FacesContext.getCurrentInstance().addMessage(null, facesMessage);
		return "home";
	}

	public String completeTask(String taskId) {
		
//		formService.submitTaskFormData(taskId, processVariables);
		
		getProcessService().completeTask(taskId);
		
		String mensagem = "Tarefa " + taskId + " completada com sucesso!";
//		FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, "Aviso", mensagem);
//		FacesContext.getCurrentInstance().addMessage(null, facesMessage);
		
		return "inbox";
	}

	public List<Task> getPersonalTaskList() {
		return getProcessService().getPersonalTaskList();
	}

	public String getCurrentUserName() {
		return SecurityUtil.getCurrentUsername();
	}

	public String viewTask(String taskId) {
		Task task = getProcessService().findTaskById(taskId);
		this.setCurrentTask(task);
		getProcessService().startTask(taskId); // claim task
		TaskFormData taskFormData = formService.getTaskFormData(taskId);
		
		String formKey = taskFormData.getFormKey();
		return formKey;
	}

	public ProcessService getProcessService() {
		return processService;
	}

	public void setProcessService(ProcessService processService) {
		this.processService = processService;
	}

	public FormService getFormService() {
		return formService;
	}

	public void setFormService(FormService formService) {
		this.formService = formService;
	}

	public ProcessDefinition getCurrentProcessDefinition() {
		return currentProcessDefinition;
	}

	public void setCurrentProcessDefinition(ProcessDefinition currentProcessDefinition) {
		this.currentProcessDefinition = currentProcessDefinition;
	}

	public Task getCurrentTask() {
		return currentTask;
	}

	public void setCurrentTask(Task currentTask) {
		this.currentTask = currentTask;
	}
//
//	public Map<String, String> getProcessVariables() {
//  	return processVariables;
//  }
//
//	public void setProcessVariables(Map<String, String> processVariables) {
//  	this.processVariables = processVariables;
//  }

}